/********************************************************************************
 * CompanyProfileAction.java
 *
 * Created by Jigar Mistry
 * Last modified Date: Sep 29, 2008 4:36:58 PM by Jigar Mistry
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ********************************************************************************/
package com.printeverywhere.action;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.util.ServletContextAware;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionContext;
import com.printeverywhere.delegator.AuctionServiceDelegator;
import com.printeverywhere.delegator.CategoryManagerServiceDelegator;
import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.delegator.LocationCategoriesServiceDelegator;
import com.printeverywhere.delegator.TranslationSystemServiceDelegator;
import com.printeverywhere.dto.AdditionalCategories;
import com.printeverywhere.dto.AdditionalOptionsFees;
import com.printeverywhere.dto.AuctionListingFees;
import com.printeverywhere.dto.GeneralCategories;
import com.printeverywhere.dto.GeneralCategoriesWOS;
import com.printeverywhere.dto.GeneralOptions;
import com.printeverywhere.dto.ItemAuctionImage;
import com.printeverywhere.dto.ItemAuctionImageId;
import com.printeverywhere.dto.ItemCategories;
import com.printeverywhere.dto.Items;
import com.printeverywhere.dto.Itemtype;
import com.printeverywhere.dto.Languages;
import com.printeverywhere.dto.ListingOptions;
import com.printeverywhere.dto.LocationCategories;
import com.printeverywhere.dto.MiscellaneousFees;
import com.printeverywhere.dto.Options;
import com.printeverywhere.dto.ProfileData;
import com.printeverywhere.dto.ProfileDataMulti;
import com.printeverywhere.dto.Transactions;
import com.printeverywhere.dto.UploadedFile;
import com.printeverywhere.dto.User;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.CommonUtil;
import com.printeverywhere.util.DateUtil;
import com.printeverywhere.util.SearchCriteria;
import com.printeverywhere.util.SearchListCriteria;
import com.printeverywhere.util.SessionUtil;

public class CompanyProfileAction extends FrontBaseSupport implements ServletRequestAware, ServletContextAware {

	/**  */
	private static final long serialVersionUID = 1L;

	private String subCategory;

	private List<GeneralCategories> listGeneralCategories = new ArrayList<GeneralCategories>();

	private List<Languages> languageList;

	/** The region list. */
	private List<LocationCategories> regionList = getRegionListByLevel();

	private List<LocationCategories> countryList = getRegionListByLevel();

	private List<LocationCategories> stateList = getRegionListByLevel();

	private List<Options> businessTypesList = getOptionsById(1);

	private List<Options> noOfEmployeesList = getOptionsById(2);

	private List<Options> registeredCapitalList = getOptionsById(3);

	private List<Options> ownerTypeList = getOptionsById(4);

	private List<Options> annualSalesVolumeList = getOptionsById(5);

	private List<Options> exportPercentageList = getOptionsById(6);

	private List<Options> annualPurchaseVolumeList = getOptionsById(7);

	private List<Options> factorySizeList = getOptionsById(8);

	private List<Options> researchAndDevelopmentStaffList = getOptionsById(9);

	private List<Options> qualityControlStaffList = getOptionsById(10);

	private List<Options> managementCertificationList = getOptionsById(11);

	private List<Options> contractManufacturingList = getOptionsById(12);

	private List<Items> featuredItems = new ArrayList<Items>();

	private Items items;

	private double fees;

	private double fileFees;

	private double regularInsertionFees;

	private File upload;

	private String uploadContentType;

	private String uploadFileName;

	private ProfileData profileData;

	private ProfileDataMulti profileDataMulti;

	private String homePageUrl;

	private HttpServletRequest request;

	private List<UploadedFile> uploadedFiles = new ArrayList<UploadedFile>();

	private String action = "submitCreateProfile";

	private String[] categories;

	private String[] backCategories;

	private String backAction;

	private Integer thumbnailId;

	private Integer gallaryId;

	private ServletContext servletContext;

	public ServletContext getServletContext() {
		return servletContext;
	}

	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

	public String getAction() {
		return action;
	}

	public String getHomePageUrl() {
		return homePageUrl;
	}

	public void setHomePageUrl(String homePageUrl) {
		this.homePageUrl = homePageUrl;
	}

	public CompanyProfileAction() {
		super(false, false);
		setMyAccount(true);
	}

	public String execute(){
		return SUCCESS;
	}

	public String backAction() {
		this.items = (Items) ActionContext.getContext().getSession().get("items");
		this.categories = (String[]) ActionContext.getContext().getSession().get("categories");
		this.backCategories = (String[]) ActionContext.getContext().getSession().get("backCategories");
		this.uploadedFiles = (List<UploadedFile>) ActionContext.getContext().getSession().get("uploadedFiles");

		countryList = LocationCategoriesServiceDelegator.getCountryListByRegionId(this.items.getLocationCategoriesByRegion().getCategoryId());
		stateList = LocationCategoriesServiceDelegator.getStateListByCountryId(this.items.getLocationCategoriesByCountry().getCategoryId());
		languageList  = (List<Languages>) ActionContext.getContext().getApplication().get(AdminConstants.LANGUAGES);

		return SUCCESS;
	}

	public String featuredCompanyProfile() {
		setLeftAllowed(true);
		setRightAllowed(true);
		setCategoryDisplay(true);
		setSubCategories();

		List<Criterion> criterionList = new ArrayList<Criterion>();
		criterionList.add(Restrictions.eq("categoryFeatured", Boolean.TRUE));
		criterionList.add(Restrictions.ge("endTime", DateUtil.getTodaysDate()));
		criterionList.add(Restrictions.eq("itemtype.id", 6));

		if(request.getSession().getAttribute("categoryInSession") != null) {
			GeneralCategoriesWOS category = (GeneralCategoriesWOS) CommonFunctionDelegator.getObjectById("categoryId", Integer.parseInt(request.getSession().getAttribute("categoryInSession").toString()), GeneralCategoriesWOS.class);
			List<Criterion> tmpCriterionList = new ArrayList<Criterion>();
			tmpCriterionList.add(Restrictions.ge("categoryId", category.getCategoryId()));
			tmpCriterionList.add(Restrictions.eq("mainParentId", category.getMainParentId()));
			criterionList.add(Restrictions.in("generalCategories", CommonFunctionDelegator.getObjectsByCriterion(tmpCriterionList, null, null, GeneralCategoriesWOS.class)));
		}

		this.featuredItems = (List<Items>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, null, null, Items.class);

		return SUCCESS;
	}

	public String createCompanyProfile() {
		setLeftAllowed(false);
		setMyAccount(false);

		ActionContext.getContext().getSession().put("uploadedFiles", null);

		return SUCCESS;
	}

	public String uploadFile() {
		setLeftAllowed(false);
		setMyAccount(false);

		if(request.getParameter("deleteAction") != null) {
			if(ActionContext.getContext().getSession().get("uploadedFiles") != null) {
				uploadedFiles = (List<UploadedFile>) ActionContext.getContext().getSession().get("uploadedFiles");
				uploadedFiles.remove(Integer.parseInt(request.getParameter("deleteAction")));
				ActionContext.getContext().getSession().put("uploadedFiles", uploadedFiles);
			}
		} else {
			File tempFile = null;
			try {
				if(ActionContext.getContext().getSession().get("uploadedFiles") != null) {
					uploadedFiles = (List<UploadedFile>) ActionContext.getContext().getSession().get("uploadedFiles");
				}

				int totalFileSize = 0;
				for (UploadedFile uploadedFile : uploadedFiles) {
					totalFileSize += (uploadedFile.getSize() / 1024);
				}
				ListingOptions listingOptions = (ListingOptions) ActionContext.getContext().getApplication().get(AdminConstants.LISTING_OPTIONS);
				List<AdditionalOptionsFees> additionalOptionsFeesList = (List<AdditionalOptionsFees>) ActionContext.getContext().getApplication().get(AdminConstants.OPTIONS_FEES);

				if(uploadedFiles.size() >= listingOptions.getNumberOfFilesAllowedToUpload()) {
					addActionError("You can not upload more than " + listingOptions.getNumberOfFilesAllowedToUpload() + " files.");
				} else if(upload != null && (upload.length() / 1024) > listingOptions.getMaximumUploadFileSize()) {
					addActionError("You can not upload file with size more than " + listingOptions.getMaximumUploadFileSize() + " Kb.");
				}  else if(listingOptions.getAllowAnyFileTypeToUpload().equalsIgnoreCase("false") && !isCorrectFileExtention(listingOptions.getFilesExtensionsAllowed(), getUploadFileName())) {
					addActionError("You can not upload file other than " + listingOptions.getFilesExtensionsAllowed());
				} else if(totalFileSize > listingOptions.getMaximumUploadTotalFilesSize()) {
					addActionError("Your total upload size exeeds " + listingOptions.getFilesExtensionsAllowed() + " Kb.");
				} else {
					String method = "getAuction";
					int type = Integer.parseInt(request.getParameter("type"));
					if(type == 2)
						method = "getFixedPrice";
					else if(type == 3)
						method = "getShop";

					UploadedFile uploadedFile = new UploadedFile();
					tempFile = File.createTempFile("MyFile.txt", ".tmp" );
					FileUtils.copyFile(upload, tempFile);
					uploadedFile.setName(tempFile.getName());
					uploadedFile.setSize(tempFile.length());
					uploadedFile.setContentType(getUploadContentType());
					uploadedFiles.add(uploadedFile);
					if(uploadedFiles.size() == 1) {
						this.fileFees = Double.valueOf(CommonUtil.callMethod(AdditionalOptionsFees.class, additionalOptionsFeesList.get(10), method).toString());
					} else if(uploadedFiles.size() == 2) {
						this.fileFees = Double.valueOf(CommonUtil.callMethod(AdditionalOptionsFees.class, additionalOptionsFeesList.get(11), method).toString());
					} else if(uploadedFiles.size() == 3) {
						this.fileFees = Double.valueOf(CommonUtil.callMethod(AdditionalOptionsFees.class, additionalOptionsFeesList.get(12), method).toString());
					} else if(uploadedFiles.size() == 4) {
						this.fileFees = Double.valueOf(CommonUtil.callMethod(AdditionalOptionsFees.class, additionalOptionsFeesList.get(13), method).toString());
					} else if(uploadedFiles.size() >= 5) {
						this.fileFees = Double.valueOf(CommonUtil.callMethod(AdditionalOptionsFees.class, additionalOptionsFeesList.get(14), method).toString());
					}
					ActionContext.getContext().getSession().put("uploadedFiles", uploadedFiles);
					System.out.println("Path : " + tempFile.getAbsolutePath());
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return INPUT;
	}

	@SuppressWarnings("unchecked")
	public String previewProfile() {
		setLeftAllowed(false);
		setMyAccount(false);

		this.backAction = "backToProfile";

		String[] categories = (String[]) ActionContext.getContext().getSession().get("categories");
		this.backCategories = this.categories;

		for (int index = 0; index < categories.length; index++) {
			DynamicTreeSelectAction dynamicTreeSelectAction = new DynamicTreeSelectAction();
			String[] categoryList = dynamicTreeSelectAction.getGeneralCategoriesFromCategoryId(Integer.parseInt(categories[index]));
			categories[index] = "";
			for (String str : categoryList) {
				categories[index] += (str + "->");
			}
			categories[index] = categories[index].substring(0, categories[index].length() - 2);
		}

		if(ActionContext.getContext().getSession().get("uploadedFiles") != null) {
			this.uploadedFiles = (List<UploadedFile>) ActionContext.getContext().getSession().get("uploadedFiles");
		}

		try {
			if (this.thumbnailId != null) {
				File file = new File(System.getProperty("java.io.tmpdir") + "\\" + this.uploadedFiles.get(this.thumbnailId).getName());
				this.items.setThumbImage(CommonUtil.getBytesFromFile(file));
			}

			if (this.gallaryId != null) {
				File file = new File(System.getProperty("java.io.tmpdir") + "\\" + this.uploadedFiles.get(this.thumbnailId).getName());
				this.items.setGalleryImage(CommonUtil.getBytesFromFile(file));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		this.items.setLocationCategoriesByRegion(LocationCategoriesServiceDelegator.getWOS(this.items.getLocationCategoriesByRegion().getCategoryId()));
		this.items.setLocationCategoriesByCountry(LocationCategoriesServiceDelegator.getWOS(this.items.getLocationCategoriesByCountry().getCategoryId()));
		this.items.setLocationCategoriesByState(LocationCategoriesServiceDelegator.getWOS(this.items.getLocationCategoriesByState().getCategoryId()));
		this.items.setItemLanguage(TranslationSystemServiceDelegator.get(this.items.getItemLanguage().getLanguageCode()));

		ActionContext.getContext().getSession().put("items", this.items);
		ActionContext.getContext().getSession().put("profileData", this.profileData);
		ActionContext.getContext().getSession().put("categories", request.getParameterValues("categoryId"));
		ActionContext.getContext().getSession().put("backCategories", this.backCategories);

		feesCalculation();

		return SUCCESS;
	}

	/**
	 * Gets the country list by level.
	 *
	 * @return the country list by level
	 */
	public List<LocationCategories> getRegionListByLevel() {
		SearchListCriteria searchListCriteria = new SearchListCriteria();

		searchListCriteria.setOrderBy("categoryName");
		searchListCriteria.setAsc(true);
		searchListCriteria.setAttribute(AdminConstants.LEVEL, new Object[] {
				AdminConstants.ONE_NUMBER, searchListCriteria.EQ });
		return LocationCategoriesServiceDelegator.getList(searchListCriteria);
	}

	public List<Options> getOptionsById(int id) {
		SearchCriteria searchCriteria = new SearchCriteria();

		searchCriteria.setAttribute("groupId", id);
		return (List<Options>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, Options.class);
	}

	public List<LocationCategories> getRegionList() {
		return regionList;
	}

	public void setRegionList(List<LocationCategories> regionList) {
		this.regionList = regionList;
	}

	public List<Options> getBusinessTypesList() {
		return businessTypesList;
	}

	public void setBusinessTypesList(List<Options> businessTypesList) {
		this.businessTypesList = businessTypesList;
	}

	public List<Options> getNoOfEmployeesList() {
		return noOfEmployeesList;
	}

	public void setNoOfEmployeesList(List<Options> noOfEmployeesList) {
		this.noOfEmployeesList = noOfEmployeesList;
	}

	public List<Options> getRegisteredCapitalList() {
		return registeredCapitalList;
	}

	public void setRegisteredCapitalList(List<Options> registeredCapitalList) {
		this.registeredCapitalList = registeredCapitalList;
	}

	public List<Options> getOwnerTypeList() {
		return ownerTypeList;
	}

	public void setOwnerTypeList(List<Options> ownerTypeList) {
		this.ownerTypeList = ownerTypeList;
	}

	public List<Options> getAnnualSalesVolumeList() {
		return annualSalesVolumeList;
	}

	public void setAnnualSalesVolumeList(List<Options> annualSalesVolumeList) {
		this.annualSalesVolumeList = annualSalesVolumeList;
	}

	public List<Options> getExportPercentageList() {
		return exportPercentageList;
	}

	public void setExportPercentageList(List<Options> exportPercentageList) {
		this.exportPercentageList = exportPercentageList;
	}

	public List<Options> getAnnualPurchaseVolumeList() {
		return annualPurchaseVolumeList;
	}

	public void setAnnualPurchaseVolumeList(List<Options> annualPurchaseVolumeList) {
		this.annualPurchaseVolumeList = annualPurchaseVolumeList;
	}

	public List<Options> getFactorySizeList() {
		return factorySizeList;
	}

	public void setFactorySizeList(List<Options> factorySizeList) {
		this.factorySizeList = factorySizeList;
	}

	public List<Options> getResearchAndDevelopmentStaffList() {
		return researchAndDevelopmentStaffList;
	}

	public void setResearchAndDevelopmentStaffList(
			List<Options> researchAndDevelopmentStaffList) {
		this.researchAndDevelopmentStaffList = researchAndDevelopmentStaffList;
	}

	public List<Options> getQualityControlStaffList() {
		return qualityControlStaffList;
	}

	public void setQualityControlStaffList(List<Options> qualityControlStaffList) {
		this.qualityControlStaffList = qualityControlStaffList;
	}

	public List<Options> getManagementCertificationList() {
		return managementCertificationList;
	}

	public void setManagementCertificationList(
			List<Options> managementCertificationList) {
		this.managementCertificationList = managementCertificationList;
	}

	public List<Options> getContractManufacturingList() {
		return contractManufacturingList;
	}

	public void setContractManufacturingList(List<Options> contractManufacturingList) {
		this.contractManufacturingList = contractManufacturingList;
	}

	public String addCompanyProfile() {
		GeneralOptions generalOptions = (GeneralOptions) ActionContext.getContext().getApplication().get(AdminConstants.GENERAL_OPTIONS);
		User tmpUser = SessionUtil.getUser(this.request);
		fees = Double.valueOf(request.getParameter("feesCharge"));

		items = (Items) ActionContext.getContext().getSession().get("items");
		if(items == null){
			addActionMessage("You have alreday submitted this item once");
			return INPUT;
		}

		if(generalOptions.getMaximumAllowedNegativeBalance() > (tmpUser.getCurrentBalance() - fees)) {
			addActionMessage("You do not have enough balance.");
		} else {
			this.items = (Items) ActionContext.getContext().getSession().get("items");
			this.items.setUserId(tmpUser);
			this.items.setStartTime(DateUtil.getTodaysDate());
			this.items.setEndTime(DateUtil.addDate(this.items.getStartTime(), Calendar.MONTH, Integer.parseInt(this.items.getDuration())));

			SearchCriteria searchCriteria = new SearchCriteria();
			searchCriteria.setAttribute("id", 6);
			List<Itemtype> itemTypeList = (List<Itemtype>) CommonFunctionDelegator
					.getObjectBySearchCriteria(searchCriteria, Itemtype.class);
			if (itemTypeList != null)
				this.items.setItemtype((Itemtype) itemTypeList.get(0));
			AuctionServiceDelegator.saveAuctionSell(items);

			Transactions transactions = new Transactions();
			transactions.setAmount(fees);
			transactions.setBalance(tmpUser.getCurrentBalance() - fees);
			transactions.setDate(DateUtil.getTodaysDate());
			transactions.setTitle(this.items.getTitle());
			transactions.setUser(tmpUser);
			transactions.setAmount(fees);
			CommonFunctionDelegator.generalInsertbyobj(transactions);

			if(ActionContext.getContext().getSession().get("uploadedFiles") != null) {
				uploadedFiles = (List<UploadedFile>) ActionContext.getContext().getSession().get("uploadedFiles");
				String path = servletContext.getRealPath("/upload");
				for (UploadedFile uploadedFile : uploadedFiles) {
					File srcFile = new File(System.getProperty("java.io.tmpdir") + "\\" + uploadedFile.getName());
					File destFile = new File(path + "\\" + uploadedFile.getName());

					try {
						FileUtils.copyFile(srcFile, destFile);
					} catch (IOException e) {
						e.printStackTrace();
					}

					ItemAuctionImageId itemAuctionImage = new ItemAuctionImageId();
					itemAuctionImage.setItems(this.items);
					itemAuctionImage.setUser(tmpUser);
					itemAuctionImage.setImagePath("upload/" + uploadedFile.getName());
					itemAuctionImage.setContentType(uploadedFile.getContentType());

					ItemAuctionImage itemAuctionImage2 = new ItemAuctionImage();
					itemAuctionImage2.setId(itemAuctionImage);
					CommonFunctionDelegator.generalInsertbyobj(itemAuctionImage2);
				}
			}

			String[] categories = (String[]) ActionContext.getContext().getSession().get("categories");
			for (String categoryId : categories) {
				ItemCategories itemCategories = new ItemCategories();
				itemCategories.setCategoryId(CategoryManagerServiceDelegator.getWOS(Integer.parseInt(categoryId)));
				itemCategories.setItemId(this.items);
				CommonFunctionDelegator.generalInsertbyobj(itemCategories);
			}
			SessionUtil.getUser(request).setCurrentBalance(SessionUtil.getUser(request).getCurrentBalance() - fees);
			CommonFunctionDelegator.generalUpdatebyobj(SessionUtil.getUser(request));
			request.getSession().setAttribute("itemId", this.items.getItemId());
			ActionContext.getContext().getSession().put("items", null);
		}
		return SUCCESS;
	}

	public Items getItems() {
		return items;
	}

	public void setItems(Items items) {
		this.items = items;
	}

	public ProfileData getProfileData() {
		return profileData;
	}

	public void setProfileData(ProfileData profileData) {
		this.profileData = profileData;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public String getSubCategory() {
		return subCategory;
	}

	public List<GeneralCategories> getListGeneralCategories() {
		return listGeneralCategories;
	}

	private void setSubCategories() {
		if(request.getSession().getAttribute("categoryInSession") != null) {
			SearchCriteria searchCriteria = new SearchCriteria();
			searchCriteria.setAttribute("generalCategories.categoryId", Integer.parseInt(request.getSession().getAttribute("categoryInSession").toString()));
			List<GeneralCategories> tmpGeneralCategories = (List<GeneralCategories>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, GeneralCategories.class);

			boolean flag = false;
			for (GeneralCategories generalCategories : tmpGeneralCategories) {

				if(flag) {
					searchCriteria.removeAllAttribute();
					searchCriteria.setAttribute("generalCategories.categoryId", generalCategories.getCategoryId());
					generalCategories.setGeneralCategoriesList((List<GeneralCategories>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, GeneralCategories.class));
					this.listGeneralCategories.add(generalCategories);
				} else {
					this.subCategory = generalCategories.getCategoryName();
					flag = true;
				}
			}
		}
	}

	@Override
	public void validate() {
		super.validate();

		List<AdditionalCategories> additionalCategoriesList = (List<AdditionalCategories>) ActionContext.getContext().getApplication().get(AdminConstants.CATEGORY_FEES);
		ListingOptions listingOptions = (ListingOptions) ActionContext.getContext().getApplication().get(AdminConstants.LISTING_OPTIONS);
		String[] words = listingOptions.getBannedWords().split(",");

		if(this.items.getTitle().length() > listingOptions.getMaximumTitleSizeInBytes().intValue()) {
			addFieldError("items.title", "Title length can't be more than " + listingOptions.getMaximumTitleSizeInBytes().intValue());
		} else if (CommonUtil.isContainsBannedWords(words, this.items.getTitle())) {
			addFieldError("items.title", "Title contains banned word(s).");
		}

		if(this.items.getSubTitle().length() > listingOptions.getShortTitleSize().intValue()) {
			addFieldError("items.subTitle", "Sub-Title length can't be more than " + listingOptions.getShortTitleSize().intValue());
		} else if (CommonUtil.isContainsBannedWords(words, this.items.getSubTitle())) {
			addFieldError("items.subTitle", "Sub-Title contains banned word(s).");
		}

		if(this.items.getDescription().length() > listingOptions.getMaximumDescriptionSizeInBytes().intValue()) {
			addFieldError("items.description", "Description length can't be more than " + listingOptions.getMaximumDescriptionSizeInBytes().intValue());
		} else if (CommonUtil.isContainsBannedWords(words, this.items.getDescription())) {
			addFieldError("items.description", "Description contains banned word(s).");
		}

		if(request.getParameter("type") != null) {
			int type = Integer.parseInt(request.getParameter("type"));

			if(type == 6) {
				if(request.getParameterValues("categoryId").length > (int) additionalCategoriesList.get(0).getCompanyProfile().doubleValue()) {
					addActionError("You can not select more than " + (int) additionalCategoriesList.get(0).getCompanyProfile().doubleValue() + " Categories.");
				}
			}
		}
	}

	private void feesCalculation() {
		this.fees = 0;
		int totalFiles = this.uploadedFiles.size();
		String method = "getCompanyProfile";

		List<AdditionalOptionsFees> additionalOptionsFeesList = (List<AdditionalOptionsFees>) ActionContext.getContext().getApplication().get(AdminConstants.OPTIONS_FEES);
		List<AdditionalCategories> additionalCategoriesFeesList = (List<AdditionalCategories>) ActionContext.getContext().getApplication().get(AdminConstants.CATEGORY_FEES);
		List<AuctionListingFees> auctionListingFeesList = (List<AuctionListingFees>) ActionContext.getContext().getApplication().get(AdminConstants.REGULAR_AUCTION_OPTIONS);
		MiscellaneousFees miscellaneousFees = (MiscellaneousFees) ActionContext.getContext().getApplication().get(AdminConstants.MISCELLANEOUS_FEES);

		this.regularInsertionFees += (miscellaneousFees.getCompanyProfileFees() * (Integer.parseInt(this.items.getDuration()) / 30));

		if(this.items.getHomePageFeatured()) {
			this.fees += Double.valueOf(CommonUtil.callMethod(AdditionalOptionsFees.class, additionalOptionsFeesList.get(0), method).toString());
		}
		if(this.items.getCategoryFeatured()) {
			this.fees += Double.valueOf(CommonUtil.callMethod(AdditionalOptionsFees.class, additionalOptionsFeesList.get(1), method).toString());
		}
		if(this.items.getGallery() != null && this.items.getGallery()) {
			this.fees += Double.valueOf(CommonUtil.callMethod(AdditionalOptionsFees.class, additionalOptionsFeesList.get(2), method).toString());
		}
		if(this.items.getGalleryFeatured() != null && this.items.getGalleryFeatured()) {
			this.fees += Double.valueOf(CommonUtil.callMethod(AdditionalOptionsFees.class, additionalOptionsFeesList.get(3), method).toString());
		}
		if(this.items.getGalleryPlus() != null && this.items.getGalleryPlus()) {
			this.fees += Double.valueOf(CommonUtil.callMethod(AdditionalOptionsFees.class, additionalOptionsFeesList.get(4), method).toString());
		}
		if(this.items.getSubTitle().length() > 0) {
			this.fees += Double.valueOf(CommonUtil.callMethod(AdditionalOptionsFees.class, additionalOptionsFeesList.get(5), method).toString());
		}
		if(this.items.getBold()) {
			this.fees += Double.valueOf(CommonUtil.callMethod(AdditionalOptionsFees.class, additionalOptionsFeesList.get(6), method).toString());
		}
		if(this.items.getColored().length() > 0) {
			this.fees += Double.valueOf(CommonUtil.callMethod(AdditionalOptionsFees.class, additionalOptionsFeesList.get(7), method).toString());
		}
		if(this.items.getBgColor().length() > 0) {
			this.fees += Double.valueOf(CommonUtil.callMethod(AdditionalOptionsFees.class, additionalOptionsFeesList.get(8), method).toString());
		}

		if(totalFiles == 1) {
			this.fees += Double.valueOf(CommonUtil.callMethod(AdditionalOptionsFees.class, additionalOptionsFeesList.get(10), method).toString());
		} else if(totalFiles == 2) {
			this.fees += Double.valueOf(CommonUtil.callMethod(AdditionalOptionsFees.class, additionalOptionsFeesList.get(11), method).toString());
		} else if(totalFiles == 3) {
			this.fees += Double.valueOf(CommonUtil.callMethod(AdditionalOptionsFees.class, additionalOptionsFeesList.get(12), method).toString());
		} else if(totalFiles == 4) {
			this.fees += Double.valueOf(CommonUtil.callMethod(AdditionalOptionsFees.class, additionalOptionsFeesList.get(13), method).toString());
		} else if(totalFiles >= 5) {
			this.fees += Double.valueOf(CommonUtil.callMethod(AdditionalOptionsFees.class, additionalOptionsFeesList.get(14), method).toString());
		}
		if(this.items.getMediaShow() != null && this.items.getMediaShow() == 2) {
			this.fees += Double.valueOf(CommonUtil.callMethod(AdditionalOptionsFees.class, additionalOptionsFeesList.get(15), method).toString());
		}
		if(this.items.getHomePage().length() > 0) {
			this.fees += Double.valueOf(CommonUtil.callMethod(AdditionalOptionsFees.class, additionalOptionsFeesList.get(16), method).toString());
		}
		if(this.items.getCrossPromotion() == null){
		} else {
			if ((!this.items.getCrossPromotion().equals(""))
					&& (!this.items.getCrossPromotion().equals(null))) {
				if (Integer.parseInt(this.items.getCrossPromotion()) == 3) {
					this.fees += Double.valueOf(CommonUtil.callMethod(AdditionalOptionsFees.class, additionalOptionsFeesList.get(17), method).toString());
				} else if (Integer.parseInt(this.items.getCrossPromotion()) == 6) {
					this.fees += Double.valueOf(CommonUtil.callMethod(AdditionalOptionsFees.class, additionalOptionsFeesList.get(18), method).toString());
				} else if (Integer.parseInt(this.items.getCrossPromotion()) == 9) {
					this.fees += Double.valueOf(CommonUtil.callMethod(AdditionalOptionsFees.class, additionalOptionsFeesList.get(19), method).toString());
				}
			}
		}

		if(this.items.getFramed() != null && this.items.getFramed()) {
			this.fees += Double.valueOf(CommonUtil.callMethod(AdditionalOptionsFees.class, additionalOptionsFeesList.get(21), method).toString());
		}

		double categoryFee = 0;
		if(request.getParameterValues("categoryId") == null){

		}else {
		for (String category : request.getParameterValues("categoryId")) {
			int level = CategoryManagerServiceDelegator.get(Integer.parseInt(category)).getLevel();
			if(level == 2)
				categoryFee += (((this.regularInsertionFees + this.fees) * Double.valueOf(CommonUtil.callMethod(AdditionalCategories.class, additionalCategoriesFeesList.get(1), method).toString())) / 100);
			else if(level == 3)
				categoryFee += (((this.regularInsertionFees + this.fees) * Double.valueOf(CommonUtil.callMethod(AdditionalCategories.class, additionalCategoriesFeesList.get(2), method).toString())) / 100);
			if(level > 3)
				categoryFee += (((this.regularInsertionFees + this.fees) * Double.valueOf(CommonUtil.callMethod(AdditionalCategories.class, additionalCategoriesFeesList.get(3), method).toString())) / 100);
		}}

		this.fees += categoryFee;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public List<UploadedFile> getUploadedFiles() {
		return uploadedFiles;
	}

	public void setUploadedFiles(List<UploadedFile> uploadedFiles) {
		this.uploadedFiles = uploadedFiles;
	}

	public ProfileDataMulti getProfileDataMulti() {
		return profileDataMulti;
	}

	public void setProfileDataMulti(ProfileDataMulti profileDataMulti) {
		this.profileDataMulti = profileDataMulti;
	}

	public double getFees() {
		return fees;
	}

	public double getRegularInsertionFees() {
		return regularInsertionFees;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public List<Items> getFeaturedItems() {
		return featuredItems;
	}

	private boolean isCorrectFileExtention(String extentions, String fileExtention) {
		boolean flag = false;

		if(fileExtention == null)
			return false;

		String str = fileExtention.substring(fileExtention.lastIndexOf(".") + 1, fileExtention.length());
		String[] tmpExtentions = extentions.split(" ");
		for (String tmpExtention : tmpExtentions) {
			if(tmpExtention.equalsIgnoreCase(str)) {
				flag = true;
				break;
			}
		}
		return flag;
	}

	public double getFileFees() {
		return fileFees;
	}

	public String[] getCategories() {
		return categories;
	}

	public Integer getThumbnailId() {
		return thumbnailId;
	}

	public void setThumbnailId(Integer thumbnailId) {
		this.thumbnailId = thumbnailId;
	}

	public Integer getGallaryId() {
		return gallaryId;
	}

	public void setGallaryId(Integer gallaryId) {
		this.gallaryId = gallaryId;
	}

	public String[] getBackCategories() {
		return backCategories;
	}

	public String getBackAction() {
		return backAction;
	}

	public List<LocationCategories> getCountryList() {
		return countryList;
	}

	public List<LocationCategories> getStateList() {
		return stateList;
	}

	public List<Languages> getLanguageList() {
		return languageList;
	}
}
